package bb.lanxing.view.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import bb.lanxing.R;
import bb.lanxing.util.DialogUtil;

public class NumberUnitPickerDialog implements View.OnClickListener {
    private AlertDialog alertDialog;
    private float defaultValue;
    private float maxNumber;
    private float miniNumber;
    private NumberPickListener numberPickListener;
    private NumberPicker numberPickerDecimal;
    private NumberPicker numberPickerInteger;
    private FrameLayout panelDecimal;
    private int rightRange;
    private TextView txtClose;
    private TextView txtLeftUnit;
    private TextView txtRightUnit;
    private int width;
    private int integer = 0;
    private int decimal = 0;

    public NumberUnitPickerDialog(Context context) {
        this.width = 0;
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_number_unit_picker, (ViewGroup) null);
        this.txtClose = inflate.findViewById(R.id.txt_close);
        this.txtLeftUnit = inflate.findViewById(R.id.txt_left_unit);
        this.txtRightUnit = inflate.findViewById(R.id.txt_right_unit);
        this.numberPickerInteger = inflate.findViewById(R.id.numberPickerInteger);
        this.numberPickerDecimal = inflate.findViewById(R.id.numberPickerDecimal);
        this.panelDecimal = inflate.findViewById(R.id.panel_decimal);
        (inflate.findViewById(R.id.tvCancel)).setOnClickListener(this);
        (inflate.findViewById(R.id.tvDone)).setOnClickListener(this);
        this.txtClose.setOnClickListener(this);
        // android.widget.NumberPicker.OnValueChangeListener
        this.numberPickerInteger
                .setOnValueChangedListener((picker, oldVal, newVal) -> NumberUnitPickerDialog.this.integer = newVal);
        // android.widget.NumberPicker.OnValueChangeListener
        this.numberPickerDecimal
                .setOnValueChangedListener((picker, oldVal, newVal) -> NumberUnitPickerDialog.this.decimal = newVal);
        // android.content.DialogInterface.OnCancelListener
        AlertDialog create =
                new AlertDialog.Builder(context).setCancelable(true).setView(inflate).setOnCancelListener(dialog -> {
                    if (NumberUnitPickerDialog.this.numberPickListener != null) {
                        NumberUnitPickerDialog.this.numberPickListener.onDialogDismiss();
                    }
                }).create();
        this.alertDialog = create;
        DialogUtil.adjustDialogGravityCenter(create);
        try {
            Window window = this.alertDialog.getWindow();
            window.setGravity(80);
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = context.getResources().getDisplayMetrics().widthPixels;
            this.width = attributes.width;
            attributes.y = 0;
            window.setAttributes(attributes);
            // window.setWindowAnimations(R.style.BottomDialog_Animation);
        } catch (Exception unused) {
        }
    }

    public int getIntegerPart(float value) {
        return (int) value;
    }

    public boolean isInteger(float value) {
        return 0.0f == value - ((float) ((int) value));
    }

    public AlertDialog getAlertDialog() {
        return this.alertDialog;
    }

    public AlertDialog show() {
        this.alertDialog.show();
        adjustDialogGravityParam();
        return this.alertDialog;
    }

    public void adjustDialogGravityParam() {
        Window window = this.alertDialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = this.width;
        attributes.gravity = Gravity.BOTTOM; // Gravity.START;//
        window.setAttributes(attributes);
    }

    public AlertDialog dimiss() {
        this.alertDialog.dismiss();
        return this.alertDialog;
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            if (numberPickListener != null) {
                numberPickListener.onDialogDismiss();
            }
            dimiss();
        } else if (id == R.id.tvDone) {
            if (numberPickListener != null) {
                numberPickListener.onNumberPick(this.integer, this.decimal);
            }
            dimiss();
        } else {
            if (id == R.id.txt_close && (numberPickListener != null)) {
                numberPickListener.onNumberPick(0.0f, 0.0f);
            }
            dimiss();
        }
    }

    public void setUpUnitwithRange() {
        int integerPart = getIntegerPart(this.miniNumber);
        int integerPart2 = getIntegerPart(this.maxNumber);
        float f = this.defaultValue;
        if (f == 0.0f) {
            f = this.miniNumber;
        }
        setLeftRange(integerPart, integerPart2, getIntegerPart(f));
    }

    public void setUpUnitwithRange(String leftUnit, String rightUnit, int rightRange, boolean isNeedClose) {
        this.txtLeftUnit.setText(leftUnit);
        if (rightUnit == null) {
            setDecimalVisibility(View.GONE);
        } else {
            this.txtRightUnit.setText(rightUnit);
            this.rightRange = rightRange;
            setDecimalVisibility(View.VISIBLE);
            int decimalValue = (int) (getDecimalValue(this.defaultValue) * rightRange);
            this.decimal = decimalValue;
            setRightRange(0, rightRange - 1, decimalValue);
        }
        if (isNeedClose) {
            this.txtClose.setVisibility(View.VISIBLE);
        } else {
            this.txtClose.setVisibility(View.GONE);
        }
    }

    public void setLeftRange(int start, int end, int defaultValue) {
        this.numberPickerInteger.setMinValue(start);
        this.numberPickerInteger.setMaxValue(end);
        this.numberPickerInteger.setValue(defaultValue);
        if (defaultValue != 0.0f) {
            start = defaultValue;
        }
        this.integer = start;
        this.numberPickerInteger.setDescendantFocusability(393216);
    }

    public void setRightRange(int start, int end, int defaultValue) {
        this.numberPickerDecimal.setMinValue(start);
        this.numberPickerDecimal.setMaxValue(end);
        this.numberPickerDecimal.setValue(defaultValue);
        this.numberPickerDecimal.setDescendantFocusability(393216);
    }

    public void setRightDefaultValue(int defaultValue) {
        this.numberPickerDecimal.setValue(defaultValue);
        this.decimal = defaultValue;
    }

    public float getMaxNumber() {
        return this.maxNumber;
    }

    public void setMaxNumber(float maxNumber) {
        this.maxNumber = maxNumber;
        setUpUnitwithRange();
    }

    public float getMiniNumber() {
        return this.miniNumber;
    }

    public void setMiniNumber(float miniNumber) {
        this.miniNumber = miniNumber;
        setUpUnitwithRange();
    }

    public float getDefaultValue() {
        return this.defaultValue;
    }

    public void setDefaultValue(float defaultValue) {
        this.defaultValue = defaultValue;
        setUpUnitwithRange();
    }

    public void setDecimalVisibility(int visibility) {
        this.panelDecimal.setVisibility(visibility);
    }

    public int getDecimalPart(float value) {
        String[] split = String.valueOf(value).split("\\.");
        if (split.length > 0) {
            return Integer.parseInt(split[1]);
        }
        return 0;
    }

    public String getDecimalPartStr(float value) {
        String[] split = String.valueOf(value).split("\\.");
        return split.length > 0 ? split[1] : "";
    }

    public float getDecimalValue(float value) {
        String[] split = String.valueOf(value).split("\\.");
        if (split.length > 0) {
            return Float.parseFloat("0." + split[1]);
        }
        return 0.0f;
    }

    public NumberUnitPickerDialog setNumberPickListener(NumberPickListener numberPickListener) {
        this.numberPickListener = numberPickListener;
        return this;
    }

    public interface NumberPickListener {
        void onDialogDismiss();
        void onNumberPick(float integer, float decimal);
    }
}
